package com.xy.tianyu.module.news.fragment

import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.widget.TextView
import androidx.lifecycle.rxLifeScope
import com.blankj.utilcode.util.ToastUtils
import com.google.android.material.tabs.TabLayout
import com.xy.tianyu.LazyLoadBaseFragment
import com.xy.tianyu.R
import com.xy.tianyu.module.news.adapter.NewsChannelPagerAdapter
import com.xy.tianyu.module.news.bean.NewsChannelBean
import com.xy.tianyu.network.Api
import com.xy.tianyu.network.ApiParams
import kotlinx.android.synthetic.main.fragment_news.*
import rxhttp.RxHttp
import rxhttp.toClass


/**
 * @time 2020/9/15 16:28
 * @class 描述:
 */
class NewsFragment : LazyLoadBaseFragment() {

    override fun setContentView(): Int {
        return R.layout.fragment_news
    }

    override fun lazyLoad() {
        rxLifeScope.launch {
            val newsChannelBean = getNewsChannel()
            newsChannelBean?.let {
                val dataList = newsChannelBean.data
                val fragmentList = mutableListOf<NewsChildFragment>()
                val titleList = mutableListOf<String>()
                if (dataList.isNotEmpty()){
                    for (data in dataList){
                        val c = data.c
                        if (c != "video"){
                            val fragment = NewsChildFragment()
                            val bundle = Bundle()
                            bundle.putParcelable("news_channel",data)
                            fragment.arguments = bundle
                            fragmentList.add(fragment)
                            titleList.add(data.name)
                        }
                    }

                    val newsChannelPagerAdapter = NewsChannelPagerAdapter(childFragmentManager,titleList,fragmentList)
                    vp_news_content.adapter = newsChannelPagerAdapter
                    tab_news_channel.setupWithViewPager(vp_news_content)

                    for (i in 0..titleList.size){
                        val tab = tab_news_channel.getTabAt(i)
                        tab?.setCustomView(R.layout.tab_news_item)
                        val tabTextView = tab?.customView?.findViewById<TextView>(R.id.tv_tab_news)
                        tabTextView?.text = titleList[i]
                        if (i == 0){
                            tabTextView?.setTextColor(Color.parseColor("#ffffff"))
                        }else{
                            tabTextView?.setTextColor(Color.parseColor("#000000"))
                        }
                    }

                    tab_news_channel.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
                        override fun onTabReselected(p0: TabLayout.Tab?) {

                        }

                        override fun onTabUnselected(p0: TabLayout.Tab?) {
                            val tabTextView = p0?.customView?.findViewById<TextView>(R.id.tv_tab_news)
                            tabTextView?.setTextColor(Color.parseColor("#000000"))
                        }

                        override fun onTabSelected(p0: TabLayout.Tab?) {
                            val tabTextView = p0?.customView?.findViewById<TextView>(R.id.tv_tab_news)
                            tabTextView?.setTextColor(Color.parseColor("#ffffff"))
                        }
                    })
                }
            }
        }

        iv_add_news_channel.setOnClickListener {

        }

    }




    private suspend fun getNewsChannel():NewsChannelBean{
        return RxHttp.get(Api.THREE_HUNDRED_AND_SIXTY_NEWS_BASE_URL+Api.THREE_HUNDRED_AND_SIXTY_NEWS_TAB_URL)
            .addAll(ApiParams.getNewsChannelParams())
            .toClass<NewsChannelBean>()
            .await()
    }

    override fun stopLoad() {

    }

}